package com.example.self_improvement_wall.controller;

import com.example.self_improvement_wall.vo.BasicMsg;
import com.example.self_improvement_wall.vo.BasicMsgWithData;
import com.example.self_improvement_wall.vo.SelfImprovementVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import sandau.self_improvement_wall.domain.ImproveInfo;
import sandau.self_improvement_wall.domain.ImproveInfoExample;
import sandau.self_improvement_wall.mapper.ImproveInfoMapper;

import java.util.ArrayList;
import java.util.List;

@RestController
    @RequestMapping("/api")
    public class SelfimprovementController {
    @Resource
    private ImproveInfoMapper improveInfoMapper;
    @GetMapping("/selfimprovementlist")
    public BasicMsgWithData<SelfImprovementVO> queryImproveList() {
        BasicMsgWithData<SelfImprovementVO> msgWithData = new BasicMsgWithData<>();
        ImproveInfo improveInfo = improveInfoMapper.selectByPrimaryKey(1L);
        SelfImprovementVO selfImprovementVO = new SelfImprovementVO();
        selfImprovementVO.setInfo(improveInfo.getInfo());
        selfImprovementVO.setPunish(improveInfo.getPunish());
        selfImprovementVO.setReward(improveInfo.getReward());
        msgWithData.setData(selfImprovementVO);
        return msgWithData;
        }

    @GetMapping("/selfimprovementList/{userid}")
    public BasicMsgWithData<List<SelfImprovementVO>> queryImproveList(@PathVariable long userid) {
        BasicMsgWithData<List<SelfImprovementVO>>msgWithData = new BasicMsgWithData<>();
        ImproveInfoExample example = new ImproveInfoExample();
        ImproveInfoExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userid);
        List<ImproveInfo> improveInfos = improveInfoMapper.selectByExample(example);
        List<SelfImprovementVO> selfImprovementVOS = new ArrayList<>();
        for (ImproveInfo i:improveInfos) {
            SelfImprovementVO selfImprovementVO = new SelfImprovementVO();
            selfImprovementVO.setInfo(i.getInfo());
            selfImprovementVO.setPunish(i.getPunish());
            selfImprovementVO.setReward(i.getReward());
            selfImprovementVOS.add(selfImprovementVO);
        }
        msgWithData.setData(selfImprovementVOS);
        return msgWithData;
    }
    @PostMapping("/selfimprovementlist")
    public BasicMsg submit(@RequestBody SelfImprovementVO selfImprovementVO) {
        BasicMsg msg = new BasicMsg(0,"");
        ImproveInfo improveInfo = new ImproveInfo();
        ImproveInfo selfImprovementVo = new ImproveInfo();
        improveInfo.setInfo(selfImprovementVo.getInfo());
        improveInfo.setInfo(selfImprovementVo.getPunish());
        improveInfo.setInfo(selfImprovementVo.getReward());
        improveInfo.setUserid(1L);
        improveInfoMapper.insert(improveInfo);
        return msg;
        }
    }
